package utils

import (
	"fmt"
	"os"
	"path/filepath"
)

func FileExist(path string) (os.FileInfo, error) {
	info, err := os.Stat(path)
	if err != nil {
		return nil, fmt.Errorf("file %s does not exist, err: %s\n", path, err.Error())
	}
	if info.IsDir() {
		return nil, fmt.Errorf("file %s is a directory\n", path)
	}
	return info, nil
}

func CreateFile(path string) (*os.File, error) {
	dir := filepath.Dir(path)
	info, err := os.Stat(dir)
	if err != nil || !info.IsDir() {
		err = os.MkdirAll(dir, 0755)
		if err != nil {
			return nil, err
		}
	}

	return os.Create(path)
}
